# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.cache import cache
from django.utils import simplejson
import datetime
import random
import base64
from utils import get_client, get_json

class SNSBackend(ModelBackend):
    def authenticate(self, request):
        today = datetime.date.today()
        now = datetime.datetime.now()
        client = get_client(request)
        try:
            if client.name == 'xn':
                info = client.getUsersInfo(fields = ['name', 'tinyurl', 'headurl', 'sex', 'zidou', 'university_history'])
                try:
                    university = info[0]['university_history'][0]['name']
                except:
                    university = ''
                defaults={'username': info[0]['uid'],
                          'nickname': info[0]['name'],
                          'tinyurl': info[0]['tinyurl'],
                          'headurl': info[0]['headurl'],
                          'sex': info[0]['sex'],
                          'zidou': info[0]['zidou'],
                          'university': university,
                          }
                user, created = User.objects.get_or_create(uid = info[0]['uid'],
                                                           origin = 'xiaonei',
                                                           defaults = defaults,
                                                           )
                if not created:
                    user.nickname = defaults['nickname']
                    user.tinyurl = defaults['tinyurl']
                    user.university = defaults['university']
                    user.sex = defaults['sex']
                    user.zidou = defaults['zidou']
                    user.headurl = defaults['headurl']
                    user.info_updated = now
            elif client.name == '51':
                if client.checked:
                    username = request.REQUEST['51_sig_user']
                    user, created = User.objects.get_or_create(username = '51-user-' + username,
                                                               origin = '51',
                                                               )
                    if created or user.info_updated.date() != today:
                        info = client.getUsersInfo(uids = [request.REQUEST['51_sig_user'],], fields = ['uid', 'username', 'nickname', 'sex', 'vip', 'city', 'facesmall', 'face'])
                        print info
                        defaults={'uid': info[0]['username'],
                                  'nickname': base64.standard_b64decode(info[0]['nickname']),
                                  'tinyurl': info[0]['facesmall'],
                                  'headurl': info[0]['face'],
                                  'sex': int(info[0]['sex']),
                                  'zidou': 0,
                                  'university': info[0]['city'],
                                  }
                        user.uid = defaults['uid']
                        user.nickname = defaults['nickname'] or info[0]['username']
                        user.tinyurl = defaults['tinyurl']
                        user.university = defaults['university']
                        user.sex = defaults['sex']
                        user.zidou = defaults['zidou']
                        user.headurl = defaults['headurl']
                        user.info_updated = now
                        user.save()
                else:
                    return None
        
            return user
        except Exception,e:
            print e
            return None